﻿#region Using directives

using System;
using System.Data;
using System.Data.Common;
using System.Collections;
using System.Collections.Generic;

using EAdvice.Entities;
using EAdvice.Data;

#endregion

namespace EAdvice.Data.Bases
{	
	///<summary>
	/// This class is the base class for any <see cref="AfiDetailProviderBase"/> implementation.
	/// It exposes CRUD methods as well as selecting on index, foreign keys and custom stored procedures.
	///</summary>
	public abstract partial class AfiDetailProviderBaseCore : EntityProviderBase<EAdvice.Entities.AfiDetail, EAdvice.Entities.AfiDetailKey>
	{		
		#region Get from Many To Many Relationship Functions
		#endregion	
		
		#region Delete Methods

		/// <summary>
		/// 	Deletes a row from the DataSource.
		/// </summary>
		/// <param name="transactionManager">A <see cref="TransactionManager"/> object.</param>
		/// <param name="key">The unique identifier of the row to delete.</param>
		/// <returns>Returns true if operation suceeded.</returns>
		public override bool Delete(TransactionManager transactionManager, EAdvice.Entities.AfiDetailKey key)
		{
			return Delete(transactionManager, key.LineItem);
		}
		
		/// <summary>
		/// 	Deletes a row from the DataSource.
		/// </summary>
		/// <param name="_lineItem">. Primary Key.</param>
		/// <remarks>Deletes based on primary key(s).</remarks>
		/// <returns>Returns true if operation suceeded.</returns>
		public bool Delete(System.Int32 _lineItem)
		{
			return Delete(null, _lineItem);
		}
		
		/// <summary>
		/// 	Deletes a row from the DataSource.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_lineItem">. Primary Key.</param>
		/// <remarks>Deletes based on primary key(s).</remarks>
		/// <returns>Returns true if operation suceeded.</returns>
		public abstract bool Delete(TransactionManager transactionManager, System.Int32 _lineItem);		
		
		#endregion Delete Methods
		
		#region Get By Foreign Key Functions
	
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_AFIDetail_AFI key.
		///		FK_AFIDetail_AFI Description: 
		/// </summary>
		/// <param name="_afiCode"></param>
		/// <returns>Returns a typed collection of EAdvice.Entities.AfiDetail objects.</returns>
		public TList<AfiDetail> GetByAfiCode(System.String _afiCode)
		{
			int count = -1;
			return GetByAfiCode(_afiCode, 0,int.MaxValue, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_AFIDetail_AFI key.
		///		FK_AFIDetail_AFI Description: 
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_afiCode"></param>
		/// <returns>Returns a typed collection of EAdvice.Entities.AfiDetail objects.</returns>
		/// <remarks></remarks>
		public TList<AfiDetail> GetByAfiCode(TransactionManager transactionManager, System.String _afiCode)
		{
			int count = -1;
			return GetByAfiCode(transactionManager, _afiCode, 0, int.MaxValue, out count);
		}
		
			/// <summary>
		/// 	Gets rows from the datasource based on the FK_AFIDetail_AFI key.
		///		FK_AFIDetail_AFI Description: 
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_afiCode"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		///  <param name="pageLength">Number of rows to return.</param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of EAdvice.Entities.AfiDetail objects.</returns>
		public TList<AfiDetail> GetByAfiCode(TransactionManager transactionManager, System.String _afiCode, int start, int pageLength)
		{
			int count = -1;
			return GetByAfiCode(transactionManager, _afiCode, start, pageLength, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_AFIDetail_AFI key.
		///		fkAfiDetailAfi Description: 
		/// </summary>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="_afiCode"></param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of EAdvice.Entities.AfiDetail objects.</returns>
		public TList<AfiDetail> GetByAfiCode(System.String _afiCode, int start, int pageLength)
		{
			int count =  -1;
			return GetByAfiCode(null, _afiCode, start, pageLength,out count);	
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_AFIDetail_AFI key.
		///		fkAfiDetailAfi Description: 
		/// </summary>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="_afiCode"></param>
		/// <param name="count">out parameter to get total records for query</param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of EAdvice.Entities.AfiDetail objects.</returns>
		public TList<AfiDetail> GetByAfiCode(System.String _afiCode, int start, int pageLength,out int count)
		{
			return GetByAfiCode(null, _afiCode, start, pageLength, out count);	
		}
						
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_AFIDetail_AFI key.
		///		FK_AFIDetail_AFI Description: 
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_afiCode"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="count">The total number of records.</param>
		/// <returns>Returns a typed collection of EAdvice.Entities.AfiDetail objects.</returns>
		public abstract TList<AfiDetail> GetByAfiCode(TransactionManager transactionManager, System.String _afiCode, int start, int pageLength, out int count);
		
	
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_AFIDetail_Product key.
		///		FK_AFIDetail_Product Description: 
		/// </summary>
		/// <param name="_productCode"></param>
		/// <returns>Returns a typed collection of EAdvice.Entities.AfiDetail objects.</returns>
		public TList<AfiDetail> GetByProductCode(System.String _productCode)
		{
			int count = -1;
			return GetByProductCode(_productCode, 0,int.MaxValue, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_AFIDetail_Product key.
		///		FK_AFIDetail_Product Description: 
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_productCode"></param>
		/// <returns>Returns a typed collection of EAdvice.Entities.AfiDetail objects.</returns>
		/// <remarks></remarks>
		public TList<AfiDetail> GetByProductCode(TransactionManager transactionManager, System.String _productCode)
		{
			int count = -1;
			return GetByProductCode(transactionManager, _productCode, 0, int.MaxValue, out count);
		}
		
			/// <summary>
		/// 	Gets rows from the datasource based on the FK_AFIDetail_Product key.
		///		FK_AFIDetail_Product Description: 
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_productCode"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		///  <param name="pageLength">Number of rows to return.</param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of EAdvice.Entities.AfiDetail objects.</returns>
		public TList<AfiDetail> GetByProductCode(TransactionManager transactionManager, System.String _productCode, int start, int pageLength)
		{
			int count = -1;
			return GetByProductCode(transactionManager, _productCode, start, pageLength, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_AFIDetail_Product key.
		///		fkAfiDetailProduct Description: 
		/// </summary>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="_productCode"></param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of EAdvice.Entities.AfiDetail objects.</returns>
		public TList<AfiDetail> GetByProductCode(System.String _productCode, int start, int pageLength)
		{
			int count =  -1;
			return GetByProductCode(null, _productCode, start, pageLength,out count);	
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_AFIDetail_Product key.
		///		fkAfiDetailProduct Description: 
		/// </summary>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="_productCode"></param>
		/// <param name="count">out parameter to get total records for query</param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of EAdvice.Entities.AfiDetail objects.</returns>
		public TList<AfiDetail> GetByProductCode(System.String _productCode, int start, int pageLength,out int count)
		{
			return GetByProductCode(null, _productCode, start, pageLength, out count);	
		}
						
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_AFIDetail_Product key.
		///		FK_AFIDetail_Product Description: 
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_productCode"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="count">The total number of records.</param>
		/// <returns>Returns a typed collection of EAdvice.Entities.AfiDetail objects.</returns>
		public abstract TList<AfiDetail> GetByProductCode(TransactionManager transactionManager, System.String _productCode, int start, int pageLength, out int count);
		
	
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_AFIDetail_Unit key.
		///		FK_AFIDetail_Unit Description: 
		/// </summary>
		/// <param name="_unit"></param>
		/// <returns>Returns a typed collection of EAdvice.Entities.AfiDetail objects.</returns>
		public TList<AfiDetail> GetByUnit(System.String _unit)
		{
			int count = -1;
			return GetByUnit(_unit, 0,int.MaxValue, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_AFIDetail_Unit key.
		///		FK_AFIDetail_Unit Description: 
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_unit"></param>
		/// <returns>Returns a typed collection of EAdvice.Entities.AfiDetail objects.</returns>
		/// <remarks></remarks>
		public TList<AfiDetail> GetByUnit(TransactionManager transactionManager, System.String _unit)
		{
			int count = -1;
			return GetByUnit(transactionManager, _unit, 0, int.MaxValue, out count);
		}
		
			/// <summary>
		/// 	Gets rows from the datasource based on the FK_AFIDetail_Unit key.
		///		FK_AFIDetail_Unit Description: 
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_unit"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		///  <param name="pageLength">Number of rows to return.</param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of EAdvice.Entities.AfiDetail objects.</returns>
		public TList<AfiDetail> GetByUnit(TransactionManager transactionManager, System.String _unit, int start, int pageLength)
		{
			int count = -1;
			return GetByUnit(transactionManager, _unit, start, pageLength, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_AFIDetail_Unit key.
		///		fkAfiDetailUnit Description: 
		/// </summary>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="_unit"></param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of EAdvice.Entities.AfiDetail objects.</returns>
		public TList<AfiDetail> GetByUnit(System.String _unit, int start, int pageLength)
		{
			int count =  -1;
			return GetByUnit(null, _unit, start, pageLength,out count);	
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_AFIDetail_Unit key.
		///		fkAfiDetailUnit Description: 
		/// </summary>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="_unit"></param>
		/// <param name="count">out parameter to get total records for query</param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of EAdvice.Entities.AfiDetail objects.</returns>
		public TList<AfiDetail> GetByUnit(System.String _unit, int start, int pageLength,out int count)
		{
			return GetByUnit(null, _unit, start, pageLength, out count);	
		}
						
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_AFIDetail_Unit key.
		///		FK_AFIDetail_Unit Description: 
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_unit"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="count">The total number of records.</param>
		/// <returns>Returns a typed collection of EAdvice.Entities.AfiDetail objects.</returns>
		public abstract TList<AfiDetail> GetByUnit(TransactionManager transactionManager, System.String _unit, int start, int pageLength, out int count);
		
		#endregion

		#region Get By Index Functions
		
		/// <summary>
		/// 	Gets a row from the DataSource based on its primary key.
		/// </summary>
		/// <param name="transactionManager">A <see cref="TransactionManager"/> object.</param>
		/// <param name="key">The unique identifier of the row to retrieve.</param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <returns>Returns an instance of the Entity class.</returns>
		public override EAdvice.Entities.AfiDetail Get(TransactionManager transactionManager, EAdvice.Entities.AfiDetailKey key, int start, int pageLength)
		{
			return GetByLineItem(transactionManager, key.LineItem, start, pageLength);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the primary key PK_AFIDetail_1 index.
		/// </summary>
		/// <param name="_lineItem"></param>
		/// <returns>Returns an instance of the <see cref="EAdvice.Entities.AfiDetail"/> class.</returns>
		public EAdvice.Entities.AfiDetail GetByLineItem(System.Int32 _lineItem)
		{
			int count = -1;
			return GetByLineItem(null,_lineItem, 0, int.MaxValue, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the PK_AFIDetail_1 index.
		/// </summary>
		/// <param name="_lineItem"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <remarks></remarks>
		/// <returns>Returns an instance of the <see cref="EAdvice.Entities.AfiDetail"/> class.</returns>
		public EAdvice.Entities.AfiDetail GetByLineItem(System.Int32 _lineItem, int start, int pageLength)
		{
			int count = -1;
			return GetByLineItem(null, _lineItem, start, pageLength, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the PK_AFIDetail_1 index.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_lineItem"></param>
		/// <remarks></remarks>
		/// <returns>Returns an instance of the <see cref="EAdvice.Entities.AfiDetail"/> class.</returns>
		public EAdvice.Entities.AfiDetail GetByLineItem(TransactionManager transactionManager, System.Int32 _lineItem)
		{
			int count = -1;
			return GetByLineItem(transactionManager, _lineItem, 0, int.MaxValue, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the PK_AFIDetail_1 index.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_lineItem"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <remarks></remarks>
		/// <returns>Returns an instance of the <see cref="EAdvice.Entities.AfiDetail"/> class.</returns>
		public EAdvice.Entities.AfiDetail GetByLineItem(TransactionManager transactionManager, System.Int32 _lineItem, int start, int pageLength)
		{
			int count = -1;
			return GetByLineItem(transactionManager, _lineItem, start, pageLength, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the PK_AFIDetail_1 index.
		/// </summary>
		/// <param name="_lineItem"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="count">out parameter to get total records for query</param>
		/// <remarks></remarks>
		/// <returns>Returns an instance of the <see cref="EAdvice.Entities.AfiDetail"/> class.</returns>
		public EAdvice.Entities.AfiDetail GetByLineItem(System.Int32 _lineItem, int start, int pageLength, out int count)
		{
			return GetByLineItem(null, _lineItem, start, pageLength, out count);
		}
		
				
		/// <summary>
		/// 	Gets rows from the datasource based on the PK_AFIDetail_1 index.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_lineItem"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="count">The total number of records.</param>
		/// <returns>Returns an instance of the <see cref="EAdvice.Entities.AfiDetail"/> class.</returns>
		public abstract EAdvice.Entities.AfiDetail GetByLineItem(TransactionManager transactionManager, System.Int32 _lineItem, int start, int pageLength, out int count);
						
		#endregion "Get By Index Functions"
	
		#region Custom Methods
		
		
		#endregion

		#region Helper Functions	
		
		/// <summary>
		/// Fill a TList&lt;AfiDetail&gt; From a DataReader.
		/// </summary>
		/// <param name="reader">Datareader</param>
		/// <param name="rows">The collection to fill</param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">number of rows.</param>
		/// <returns>a <see cref="TList&lt;AfiDetail&gt;"/></returns>
		public static TList<AfiDetail> Fill(IDataReader reader, TList<AfiDetail> rows, int start, int pageLength)
		{
			NetTiersProvider currentProvider = DataRepository.Provider;
            bool useEntityFactory = currentProvider.UseEntityFactory;
            bool enableEntityTracking = currentProvider.EnableEntityTracking;
            LoadPolicy currentLoadPolicy = currentProvider.CurrentLoadPolicy;
			Type entityCreationFactoryType = currentProvider.EntityCreationalFactoryType;
			
			// advance to the starting row
			for (int i = 0; i < start; i++)
			{
				if (!reader.Read())
				return rows; // not enough rows, just return
			}
			for (int i = 0; i < pageLength; i++)
			{
				if (!reader.Read())
					break; // we are done
					
				string key = null;
				
				EAdvice.Entities.AfiDetail c = null;
				if (useEntityFactory)
				{
					key = new System.Text.StringBuilder("AfiDetail")
					.Append("|").Append((System.Int32)reader[((int)AfiDetailColumn.LineItem - 1)]).ToString();
					c = EntityManager.LocateOrCreate<AfiDetail>(
					key.ToString(), // EntityTrackingKey
					"AfiDetail",  //Creational Type
					entityCreationFactoryType,  //Factory used to create entity
					enableEntityTracking); // Track this entity?
				}
				else
				{
					c = new EAdvice.Entities.AfiDetail();
				}
				
				if (!enableEntityTracking ||
					c.EntityState == EntityState.Added ||
					(enableEntityTracking &&
					
						(
							(currentLoadPolicy == LoadPolicy.PreserveChanges && c.EntityState == EntityState.Unchanged) ||
							(currentLoadPolicy == LoadPolicy.DiscardChanges && c.EntityState != EntityState.Unchanged)
						)
					))
				{
					c.SuppressEntityEvents = true;
					c.LineItem = (System.Int32)reader[((int)AfiDetailColumn.LineItem - 1)];
					c.AfiCode = (System.String)reader[((int)AfiDetailColumn.AfiCode - 1)];
					c.ProductCode = (reader.IsDBNull(((int)AfiDetailColumn.ProductCode - 1)))?null:(System.String)reader[((int)AfiDetailColumn.ProductCode - 1)];
					c.Unit = (reader.IsDBNull(((int)AfiDetailColumn.Unit - 1)))?null:(System.String)reader[((int)AfiDetailColumn.Unit - 1)];
					c.Quantity = (reader.IsDBNull(((int)AfiDetailColumn.Quantity - 1)))?null:(System.Int32?)reader[((int)AfiDetailColumn.Quantity - 1)];
					c.UnitCost = (reader.IsDBNull(((int)AfiDetailColumn.UnitCost - 1)))?null:(System.Decimal?)reader[((int)AfiDetailColumn.UnitCost - 1)];
					c.CoverPeriodFrom = (reader.IsDBNull(((int)AfiDetailColumn.CoverPeriodFrom - 1)))?null:(System.DateTime?)reader[((int)AfiDetailColumn.CoverPeriodFrom - 1)];
					c.CoverPeriodTo = (reader.IsDBNull(((int)AfiDetailColumn.CoverPeriodTo - 1)))?null:(System.DateTime?)reader[((int)AfiDetailColumn.CoverPeriodTo - 1)];
					c.EntityTrackingKey = key;
					c.AcceptChanges();
					c.SuppressEntityEvents = false;
				}
				rows.Add(c);
			}
		return rows;
		}		
		/// <summary>
		/// Refreshes the <see cref="EAdvice.Entities.AfiDetail"/> object from the <see cref="IDataReader"/>.
		/// </summary>
		/// <param name="reader">The <see cref="IDataReader"/> to read from.</param>
		/// <param name="entity">The <see cref="EAdvice.Entities.AfiDetail"/> object to refresh.</param>
		public static void RefreshEntity(IDataReader reader, EAdvice.Entities.AfiDetail entity)
		{
			if (!reader.Read()) return;
			
			entity.LineItem = (System.Int32)reader[((int)AfiDetailColumn.LineItem - 1)];
			entity.AfiCode = (System.String)reader[((int)AfiDetailColumn.AfiCode - 1)];
			entity.ProductCode = (reader.IsDBNull(((int)AfiDetailColumn.ProductCode - 1)))?null:(System.String)reader[((int)AfiDetailColumn.ProductCode - 1)];
			entity.Unit = (reader.IsDBNull(((int)AfiDetailColumn.Unit - 1)))?null:(System.String)reader[((int)AfiDetailColumn.Unit - 1)];
			entity.Quantity = (reader.IsDBNull(((int)AfiDetailColumn.Quantity - 1)))?null:(System.Int32?)reader[((int)AfiDetailColumn.Quantity - 1)];
			entity.UnitCost = (reader.IsDBNull(((int)AfiDetailColumn.UnitCost - 1)))?null:(System.Decimal?)reader[((int)AfiDetailColumn.UnitCost - 1)];
			entity.CoverPeriodFrom = (reader.IsDBNull(((int)AfiDetailColumn.CoverPeriodFrom - 1)))?null:(System.DateTime?)reader[((int)AfiDetailColumn.CoverPeriodFrom - 1)];
			entity.CoverPeriodTo = (reader.IsDBNull(((int)AfiDetailColumn.CoverPeriodTo - 1)))?null:(System.DateTime?)reader[((int)AfiDetailColumn.CoverPeriodTo - 1)];
			entity.AcceptChanges();
		}
		
		/// <summary>
		/// Refreshes the <see cref="EAdvice.Entities.AfiDetail"/> object from the <see cref="DataSet"/>.
		/// </summary>
		/// <param name="dataSet">The <see cref="DataSet"/> to read from.</param>
		/// <param name="entity">The <see cref="EAdvice.Entities.AfiDetail"/> object.</param>
		public static void RefreshEntity(DataSet dataSet, EAdvice.Entities.AfiDetail entity)
		{
			DataRow dataRow = dataSet.Tables[0].Rows[0];
			
			entity.LineItem = (System.Int32)dataRow["LineItem"];
			entity.AfiCode = (System.String)dataRow["AFICode"];
			entity.ProductCode = Convert.IsDBNull(dataRow["ProductCode"]) ? null : (System.String)dataRow["ProductCode"];
			entity.Unit = Convert.IsDBNull(dataRow["Unit"]) ? null : (System.String)dataRow["Unit"];
			entity.Quantity = Convert.IsDBNull(dataRow["Quantity"]) ? null : (System.Int32?)dataRow["Quantity"];
			entity.UnitCost = Convert.IsDBNull(dataRow["UnitCost"]) ? null : (System.Decimal?)dataRow["UnitCost"];
			entity.CoverPeriodFrom = Convert.IsDBNull(dataRow["CoverPeriodFrom"]) ? null : (System.DateTime?)dataRow["CoverPeriodFrom"];
			entity.CoverPeriodTo = Convert.IsDBNull(dataRow["CoverPeriodTo"]) ? null : (System.DateTime?)dataRow["CoverPeriodTo"];
			entity.AcceptChanges();
		}
		#endregion 
		
		#region DeepLoad Methods
		/// <summary>
		/// Deep Loads the <see cref="IEntity"/> object with criteria based of the child 
		/// property collections only N Levels Deep based on the <see cref="DeepLoadType"/>.
		/// </summary>
		/// <remarks>
		/// Use this method with caution as it is possible to DeepLoad with Recursion and traverse an entire object graph.
		/// </remarks>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="entity">The <see cref="EAdvice.Entities.AfiDetail"/> object to load.</param>
		/// <param name="deep">Boolean. A flag that indicates whether to recursively save all Property Collection that are descendants of this instance. If True, saves the complete object graph below this object. If False, saves this object only. </param>
		/// <param name="deepLoadType">DeepLoadType Enumeration to Include/Exclude object property collections from Load.</param>
		/// <param name="childTypes">EAdvice.Entities.AfiDetail Property Collection Type Array To Include or Exclude from Load</param>
		/// <param name="innerList">A collection of child types for easy access.</param>
	    /// <exception cref="ArgumentNullException">entity or childTypes is null.</exception>
	    /// <exception cref="ArgumentException">deepLoadType has invalid value.</exception>
		public override void DeepLoad(TransactionManager transactionManager, EAdvice.Entities.AfiDetail entity, bool deep, DeepLoadType deepLoadType, System.Type[] childTypes, DeepSession innerList)
		{
			if(entity == null)
				return;

			#region AfiCodeSource	
			if (CanDeepLoad(entity, "Afi|AfiCodeSource", deepLoadType, innerList) 
				&& entity.AfiCodeSource == null)
			{
				object[] pkItems = new object[1];
				pkItems[0] = entity.AfiCode;
				Afi tmpEntity = EntityManager.LocateEntity<Afi>(EntityLocator.ConstructKeyFromPkItems(typeof(Afi), pkItems), DataRepository.Provider.EnableEntityTracking);
				if (tmpEntity != null)
					entity.AfiCodeSource = tmpEntity;
				else
					entity.AfiCodeSource = DataRepository.AfiProvider.GetByAfiCode(transactionManager, entity.AfiCode);		
				
				#if NETTIERS_DEBUG
				System.Diagnostics.Debug.WriteLine("- property 'AfiCodeSource' loaded. key " + entity.EntityTrackingKey);
				#endif 
				
				if (deep && entity.AfiCodeSource != null)
				{
					innerList.SkipChildren = true;
					DataRepository.AfiProvider.DeepLoad(transactionManager, entity.AfiCodeSource, deep, deepLoadType, childTypes, innerList);
					innerList.SkipChildren = false;
				}
					
			}
			#endregion AfiCodeSource

			#region ProductCodeSource	
			if (CanDeepLoad(entity, "Product|ProductCodeSource", deepLoadType, innerList) 
				&& entity.ProductCodeSource == null)
			{
				object[] pkItems = new object[1];
				pkItems[0] = (entity.ProductCode ?? string.Empty);
				Product tmpEntity = EntityManager.LocateEntity<Product>(EntityLocator.ConstructKeyFromPkItems(typeof(Product), pkItems), DataRepository.Provider.EnableEntityTracking);
				if (tmpEntity != null)
					entity.ProductCodeSource = tmpEntity;
				else
					entity.ProductCodeSource = DataRepository.ProductProvider.GetByProductCode(transactionManager, (entity.ProductCode ?? string.Empty));		
				
				#if NETTIERS_DEBUG
				System.Diagnostics.Debug.WriteLine("- property 'ProductCodeSource' loaded. key " + entity.EntityTrackingKey);
				#endif 
				
				if (deep && entity.ProductCodeSource != null)
				{
					innerList.SkipChildren = true;
					DataRepository.ProductProvider.DeepLoad(transactionManager, entity.ProductCodeSource, deep, deepLoadType, childTypes, innerList);
					innerList.SkipChildren = false;
				}
					
			}
			#endregion ProductCodeSource

			#region UnitSource	
			if (CanDeepLoad(entity, "UnitTable|UnitSource", deepLoadType, innerList) 
				&& entity.UnitSource == null)
			{
				object[] pkItems = new object[1];
				pkItems[0] = (entity.Unit ?? string.Empty);
				UnitTable tmpEntity = EntityManager.LocateEntity<UnitTable>(EntityLocator.ConstructKeyFromPkItems(typeof(UnitTable), pkItems), DataRepository.Provider.EnableEntityTracking);
				if (tmpEntity != null)
					entity.UnitSource = tmpEntity;
				else
					entity.UnitSource = DataRepository.UnitTableProvider.GetByUnitCode(transactionManager, (entity.Unit ?? string.Empty));		
				
				#if NETTIERS_DEBUG
				System.Diagnostics.Debug.WriteLine("- property 'UnitSource' loaded. key " + entity.EntityTrackingKey);
				#endif 
				
				if (deep && entity.UnitSource != null)
				{
					innerList.SkipChildren = true;
					DataRepository.UnitTableProvider.DeepLoad(transactionManager, entity.UnitSource, deep, deepLoadType, childTypes, innerList);
					innerList.SkipChildren = false;
				}
					
			}
			#endregion UnitSource
			
			//used to hold DeepLoad method delegates and fire after all the local children have been loaded.
			Dictionary<string, KeyValuePair<Delegate, object>> deepHandles = new Dictionary<string, KeyValuePair<Delegate, object>>();
			
			//Fire all DeepLoad Items
			foreach(KeyValuePair<Delegate, object> pair in deepHandles.Values)
		    {
                pair.Key.DynamicInvoke((object[])pair.Value);
		    }
			deepHandles = null;
		}
		
		#endregion 
		
		#region DeepSave Methods

		/// <summary>
		/// Deep Save the entire object graph of the EAdvice.Entities.AfiDetail object with criteria based of the child 
		/// Type property array and DeepSaveType.
		/// </summary>
		/// <param name="transactionManager">The transaction manager.</param>
		/// <param name="entity">EAdvice.Entities.AfiDetail instance</param>
		/// <param name="deepSaveType">DeepSaveType Enumeration to Include/Exclude object property collections from Save.</param>
		/// <param name="childTypes">EAdvice.Entities.AfiDetail Property Collection Type Array To Include or Exclude from Save</param>
		/// <param name="innerList">A Hashtable of child types for easy access.</param>
		public override bool DeepSave(TransactionManager transactionManager, EAdvice.Entities.AfiDetail entity, DeepSaveType deepSaveType, System.Type[] childTypes, DeepSession innerList)
		{	
			if (entity == null)
				return false;
							
			#region Composite Parent Properties
			//Save Source Composite Properties, however, don't call deep save on them.  
			//So they only get saved a single level deep.
			
			#region AfiCodeSource
			if (CanDeepSave(entity, "Afi|AfiCodeSource", deepSaveType, innerList) 
				&& entity.AfiCodeSource != null)
			{
				DataRepository.AfiProvider.Save(transactionManager, entity.AfiCodeSource);
				entity.AfiCode = entity.AfiCodeSource.AfiCode;
			}
			#endregion 
			
			#region ProductCodeSource
			if (CanDeepSave(entity, "Product|ProductCodeSource", deepSaveType, innerList) 
				&& entity.ProductCodeSource != null)
			{
				DataRepository.ProductProvider.Save(transactionManager, entity.ProductCodeSource);
				entity.ProductCode = entity.ProductCodeSource.ProductCode;
			}
			#endregion 
			
			#region UnitSource
			if (CanDeepSave(entity, "UnitTable|UnitSource", deepSaveType, innerList) 
				&& entity.UnitSource != null)
			{
				DataRepository.UnitTableProvider.Save(transactionManager, entity.UnitSource);
				entity.Unit = entity.UnitSource.UnitCode;
			}
			#endregion 
			#endregion Composite Parent Properties

			// Save Root Entity through Provider
			if (!entity.IsDeleted)
				this.Save(transactionManager, entity);
			
			//used to hold DeepSave method delegates and fire after all the local children have been saved.
			Dictionary<string, KeyValuePair<Delegate, object>> deepHandles = new Dictionary<string, KeyValuePair<Delegate, object>>();
			//Fire all DeepSave Items
			foreach(KeyValuePair<Delegate, object> pair in deepHandles.Values)
		    {
                pair.Key.DynamicInvoke((object[])pair.Value);
		    }
			
			// Save Root Entity through Provider, if not already saved in delete mode
			if (entity.IsDeleted)
				this.Save(transactionManager, entity);
				

			deepHandles = null;
						
			return true;
		}
		#endregion
	} // end class
	
	#region AfiDetailChildEntityTypes
	
	///<summary>
	/// Enumeration used to expose the different child entity types 
	/// for child properties in <c>EAdvice.Entities.AfiDetail</c>
	///</summary>
	public enum AfiDetailChildEntityTypes
	{
		
		///<summary>
		/// Composite Property for <c>Afi</c> at AfiCodeSource
		///</summary>
		[ChildEntityType(typeof(Afi))]
		Afi,
			
		///<summary>
		/// Composite Property for <c>Product</c> at ProductCodeSource
		///</summary>
		[ChildEntityType(typeof(Product))]
		Product,
			
		///<summary>
		/// Composite Property for <c>UnitTable</c> at UnitSource
		///</summary>
		[ChildEntityType(typeof(UnitTable))]
		UnitTable,
		}
	
	#endregion AfiDetailChildEntityTypes
	
	#region AfiDetailFilterBuilder
	
	/// <summary>
	/// A strongly-typed instance of the <see cref="SqlFilterBuilder&lt;AfiDetailColumn&gt;"/> class
	/// that is used exclusively with a <see cref="AfiDetail"/> object.
	/// </summary>
	[CLSCompliant(true)]
	public class AfiDetailFilterBuilder : SqlFilterBuilder<AfiDetailColumn>
	{
		#region Constructors

		/// <summary>
		/// Initializes a new instance of the AfiDetailFilterBuilder class.
		/// </summary>
		public AfiDetailFilterBuilder() : base() { }

		/// <summary>
		/// Initializes a new instance of the AfiDetailFilterBuilder class.
		/// </summary>
		/// <param name="ignoreCase">Specifies whether to create case-insensitive statements.</param>
		public AfiDetailFilterBuilder(bool ignoreCase) : base(ignoreCase) { }

		/// <summary>
		/// Initializes a new instance of the AfiDetailFilterBuilder class.
		/// </summary>
		/// <param name="ignoreCase">Specifies whether to create case-insensitive statements.</param>
		/// <param name="useAnd">Specifies whether to combine statements using AND or OR.</param>
		public AfiDetailFilterBuilder(bool ignoreCase, bool useAnd) : base(ignoreCase, useAnd) { }

		#endregion Constructors
	}

	#endregion AfiDetailFilterBuilder
	
	#region AfiDetailParameterBuilder
	
	/// <summary>
	/// A strongly-typed instance of the <see cref="ParameterizedSqlFilterBuilder&lt;AfiDetailColumn&gt;"/> class
	/// that is used exclusively with a <see cref="AfiDetail"/> object.
	/// </summary>
	[CLSCompliant(true)]
	public class AfiDetailParameterBuilder : ParameterizedSqlFilterBuilder<AfiDetailColumn>
	{
		#region Constructors

		/// <summary>
		/// Initializes a new instance of the AfiDetailParameterBuilder class.
		/// </summary>
		public AfiDetailParameterBuilder() : base() { }

		/// <summary>
		/// Initializes a new instance of the AfiDetailParameterBuilder class.
		/// </summary>
		/// <param name="ignoreCase">Specifies whether to create case-insensitive statements.</param>
		public AfiDetailParameterBuilder(bool ignoreCase) : base(ignoreCase) { }

		/// <summary>
		/// Initializes a new instance of the AfiDetailParameterBuilder class.
		/// </summary>
		/// <param name="ignoreCase">Specifies whether to create case-insensitive statements.</param>
		/// <param name="useAnd">Specifies whether to combine statements using AND or OR.</param>
		public AfiDetailParameterBuilder(bool ignoreCase, bool useAnd) : base(ignoreCase, useAnd) { }

		#endregion Constructors
	}

	#endregion AfiDetailParameterBuilder
	
	#region AfiDetailSortBuilder
    
    /// <summary>
    /// A strongly-typed instance of the <see cref="SqlSortBuilder&lt;AfiDetailColumn&gt;"/> class
	/// that is used exclusively with a <see cref="AfiDetail"/> object.
    /// </summary>
    [CLSCompliant(true)]
    public class AfiDetailSortBuilder : SqlSortBuilder<AfiDetailColumn>
    {
		#region Constructors

		/// <summary>
		/// Initializes a new instance of the AfiDetailSqlSortBuilder class.
		/// </summary>
		public AfiDetailSortBuilder() : base() { }

		#endregion Constructors

    }    
    #endregion AfiDetailSortBuilder
	
} // end namespace
